<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCateDialog">
            添加分类
          </el-button>
        </el-col>
      </el-row>
      <!-- 树形 -->
      <tree-table
        class="treeTable"
        :show-index="true"
        index-text="#"
        border
        show-row-hover
        :expand-type="false"
        :selection-type="false"
        :data="cateList"
        :columns="columns"
      >
        <template slot="isok" slot-scope="scope">
          <i
            style="color: lightgreen"
            v-if="scope.row._isExpanded === false"
            class="el-icon-success"
          >
          </i>
          <i style="color: red" v-else class="el-icon-error"></i>
        </template>
        <!-- 排序的模板区域 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>

          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" type="primary"
            >编辑</el-button
          >
          <el-button size="mini" icon="el-icon-delete" type="danger"
            >删除</el-button
          >
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleTreeSizeChange"
        @current-change="handleTreeCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="3"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加分类对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addCateDialogVisible"
        width="50%"
        @close="cateDialogClosed"
      >
        <!-- 内容区域 -->
        <el-form
          :model="addCateForm"
          :rules="addCateFormRules"
          ref="addCateFormRef"
          label-width="100px"
        >
          <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>

          <el-form-item label="父级分类:">
            <!--options是数据源  props是配置-->
            <el-cascader
              clearable
              change-on-select
              v-model="cascaderCheck"
              :options="ParentCateList"
              :props="cascaderProp"
              @change="ParentHandleChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button @click="addCate" type="primary">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'VueShopCate',



  data() {
    return {
      //商品数据
      cateList: [],
      //查询的对象
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 3

      },
      //商品列表的绑定属性
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
          width: '400px'
        },
        {
          label: '是否有效',

          //表示将当前列 定义为模板列
          type: 'template',
          //表示当前这一列使用的模板名称
          template: 'isok'
        },
        {
          label: '排序',

          //表示将当前列 定义为模板列
          type: 'template',
          //表示当前这一列使用的模板名称
          template: 'order'
        },
        {
          label: '操作',

          //表示将当前列 定义为模板列
          type: 'template',
          //表示当前这一列使用的模板名称
          template: 'opt'
        },



      ],
      //默认总条数
      total: 0,
      // t添加分类对话框的显示与隐藏
      addCateDialogVisible: false,
      //   需要添加分类表单数据对象
      addCateForm: {
        //分类名称
        cat_name: '',
        cat_level: '',
        cat_pid: ''

      },
      //   分类对话框的信息的验证规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },

        ]
      },
      //   父级分类列表数据
      ParentCateList: [],

      //选中的分类数组
      cascaderCheck: [],
      //   指定级联选则器的配置对象
      cascaderProp: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }






    }

  },
  created() {
    this.getCateList()
  }
  ,

  mounted() {

  },

  methods: {
    async getCateList() {

      const { data: res } = await this.$http.get('categories', {
        params: this.querInfo

      })
      if (res.meta.status != 200) {
        return this.$message({ showClose: true, message: res.meta.msg, type: 'error' })

      }
      // return this.$message({ showClose: true, message: res.meta.msg, type: 'success' })

      this.cateList = res.data.result
      this.total = res.data.total
      //   console.log(this.cateList, this.total);


    },
    // 监听page-size的改变
    handleTreeSizeChange(newpagesize) {
      //   console.log(newpagesize);
      this.querInfo.pagesize = newpagesize
      this.getCateList()



    },
    //监听pagenum的改变
    handleTreeCurrentChange(newcurrent) {
      //   console.log(newcurrent);
      this.querInfo.pagenum = newcurrent
      this.getCateList()


    },
    // 显示分类对话框
    addCateDialog() {
      this.addCateDialogVisible = true
      this.getParentCateList()
    },
    // 获取父级分类的列表
    async getParentCateList() {

      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status != 200) {
        return this.$message({ showClose: true, message: res.meta.msg, type: 'error' })

      }
      this.ParentCateList = res.data


    },
    // 监听级联选择器的变化
    ParentHandleChange() {
      console.log(this.cascaderCheck);

      if (this.cascaderCheck.length > 0) {
        //父级分类的id
        this.addCateForm.cat_pid = this.cascaderCheck[this.cascaderCheck.length - 1]
        this.addCateForm.cat_level = this.cascaderCheck.length
        return
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }

    },
    //  点击添加分类
    addCate() {

      this.$refs['addCateFormRef'].validate(async (valid) => {
        if (!valid) return

        const { data: res } = await this.$http.post('categories', this.addCateForm)


        if (res.meta.status !== 201) {
          return this.$message({ showClose: true, message: res.meta.msg, type: 'error' })

        }
        this.getCateList()
        this.$message({ showClose: true, message: res.meta.msg, type: 'success' })
        this.addCateDialogVisible = false
      })

      console.log(this.addCateForm);
    },
    // 关闭会话框时清空表单,重置表单数据
    cateDialogClosed() {
      //   console.log("1111");
      this.$refs.addCateFormRef.resetFields()
      this.cascaderCheck = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    }




  },
};
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>